package com.wgf.controller;

import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgent;
import com.alibaba.cloud.ai.dashscope.agent.DashScopeAgentOptions;
import com.alibaba.cloud.ai.dashscope.api.DashScopeAgentApi;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DigitalHumanController
{
    // 百炼平台的appid,本案例是数字人Digital Human
    @Value("${spring.ai.dashscope.agent.options.app-id}")
    private String DigitalHuman_APPID;

    // 声明一个DashScopeAgent类型的私有字段，用于调用百炼平台的智能体
    private DashScopeAgent agent;

    //构造方法注入，创建百炼云端智能体对象,通过Spring的依赖注入传入DashScopeAgentApi对象，然后用它来初始化DashScopeAgent
    public DigitalHumanController(DashScopeAgentApi agentApi) {
        this.agent = new DashScopeAgent(agentApi);
    }


    /**
     * http://localhost:6022/digitalHuman
     * @param msg
     * @return
     */
    @GetMapping("/digitalHuman")
    public String digitalHuman(@RequestParam(name = "msg",defaultValue = "生成视频，内容为:\n" +
            "                卫国夫卫国夫在人工智能重塑世界的进程中,AI不仅没有淘汰Java反而为其注入了超级智能的澎湃动能,\n" +
            "                重燃Java这柄利刃的炽热锋芒！阳哥弟子班二期,也就是北京Java241229班共26人，\n" +
            "                毕业5天已经有17人成功拿到offer，就业率65%。平均月薪13412，最高年薪32万！卫国夫卫国夫\n" +
            "                大家加油。") String msg) {

        //构建DashScopeAgentOptions对象，设置appId为从配置文件中读取的DigitalHuman_APPID
        DashScopeAgentOptions options = DashScopeAgentOptions.builder().withAppId(DigitalHuman_APPID).build();

        //创建Prompt对象，将用户输入的msg作为prompt，将options作为参数
        Prompt prompt = new Prompt(msg, options);

        //调用DashScopeAgent的call方法，传入prompt对象，返回一个ChatResponse对象
        return agent.call(prompt).getResult().getOutput().getText();
    }
}